package com.wxproject.demo.Controller.Article;

import com.baomidou.mybatisplus.extension.api.R;
import com.wxproject.demo.Common.lang.ResultBody;
import com.wxproject.demo.Entity.AO.ArticleCreateAO;
import com.wxproject.demo.Entity.VO.ArticleContextVO;
import com.wxproject.demo.Service.ArticleService;
import com.wxproject.demo.Utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;

/**
 * @author HCJ
 * @date 2021/4/4 19:19
 * @description
 * 1.分页获取所有文章接口
 * 2.浏览量+1接口
 * 3.点赞量+1接口
 * 4.踩量+1接口
 * 5.根据文章ID逻辑删除接口
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 分页获取文章列表
     * @param page
     * @param size
     * @param typeId
     * @return
     */
    @PostMapping("/getAllByPage")
    public ResultBody getAllByPage(@RequestParam @NotEmpty int page,
                                                @NotEmpty int size,
                                                @NotEmpty String typeId){
        return ResultBody.ok().data("article",articleService.getAllByPage(page,size,typeId));
    }
    /**
     * 获取所有文章
     * @return
     */
    @PostMapping("/getAll")
    public ResultBody getAll(){
        return ResultBody.ok().data("chishi",articleService.getAll());
    }
    /**
     * 浏览量+1接口
     * @param articleId
     * @return
     */
    @PostMapping("/look")
    public ResultBody look(@RequestParam @NotEmpty String articleId){
        if(articleService.look(articleId)){
            return ResultBody.ok().message("浏览成功！");
        }
        return ResultBody.ok().message("浏览失败！");
    }
    /**
     * 点赞接口 点赞+1
     * @param articleId
     * @return
     */
    @PostMapping("/like")
    public ResultBody like(@RequestParam @NotEmpty String articleId){
        if(articleService.like(articleId)){
            return ResultBody.ok().message("赞成功！");
        }
        return ResultBody.ok().message("赞失败！");
    }
    /**
     * 踩接口 踩+1
     * @param articleId
     * @return
     */
    @PostMapping("/dislike")
    public ResultBody dislike(@RequestParam @NotEmpty String articleId){
        if(articleService.dislike(articleId)){
            return ResultBody.ok().message("踩成功！");
        }
        return ResultBody.ok().message("踩失败！");
    }
    /**
     * 逻辑删除文章接口
     * @param articleId
     * @return
     */
    @PostMapping("/logicDeleteArticle")
    public ResultBody logicDeleteArticle(@RequestParam @NotEmpty String articleId){
        if(articleService.logicdeleteArticle(articleId)){
            return ResultBody.ok().message("逻辑删除成功！");
        }
        return ResultBody.error().message("逻辑删除失败！");
    }
    /**
     * 删除文章将接口
     * @param articleId
     * @param rq
     * @return
     */
    @PostMapping("/DeleteArticle")
    public ResultBody DeleteArticle(@RequestParam @NotEmpty String articleId,HttpServletRequest rq){
        String userId = JWTUtil.getUserId(rq.getHeader("Token"));
        if(userId!=null){
            if(articleService.isArticleExists(articleId)){
                if(articleService.isOwnerOfArticle(articleId,userId)){
                    if(articleService.DeleteArticle(articleId)){
                        return ResultBody.ok().message("删除成功！");
                    }
                    return ResultBody.error().message("删除失败！");
                }
                return ResultBody.error().message("你没有权限删除他人发布的文章!");
            }
            return ResultBody.error().message("你要删除的文章不存在！");
        }
        return ResultBody.error().message("请先登录！");
    }
    /**
     * 热门文章推荐接口
     * @return
     */
    @PostMapping("/recommendedArticle")
    public ResultBody recommendedArticle() {
        return ResultBody.ok().data("recommend",articleService.recommendedArticle());
    }

    /**
     * 创建文章接口
     * @param articleCreateAO
     * @return
     */
    @PostMapping("/createArticle")
    public ResultBody createArticle(@RequestBody @Validated ArticleCreateAO articleCreateAO,HttpServletRequest rq) {
        String userId = JWTUtil.getUserId(rq.getHeader("Token"));
        return ResultBody.ok().data("create",articleService.createArticle(articleCreateAO,userId));
    }

    /**
     * 根据文章ID获取文章详情接口
     * @param articleId
     * @return
     */
    @PostMapping("/getArticleDetails")
    public ResultBody getArticleDetails(@RequestParam String articleId) {
        return ResultBody.ok().data("Details", articleService.getArticleDetails(articleId));
    }

    /**
     * 获取文章详情和评论
     * @param id
     * @return
     */
    @GetMapping("/getArticleContext/{id}")
    public ResultBody getArticleContext(@PathVariable("id")String id){
        ArticleContextVO articleContextVO = articleService.getArticleContext(id);
        return ResultBody.ok().data("articleContent",articleContextVO);
    }

    /**
     * 模糊查询文章
     * @param page
     * @param size
     * @param key
     * @return
     */
    @PostMapping("/searchArticles")
    public ResultBody searchArticles(@RequestParam @NotEmpty int page,
                                     @NotEmpty int size,
                                    @NotEmpty String key){
        return ResultBody.ok().data("serchArticles",articleService.serchArticles(page,size,key));
    }

    public ResultBody xxxx() {
        return ResultBody.ok();
    }
}
